<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yxw/model')->方法名();
*/
namespace jy_yxw;
defined('BY_JYA') or exit('error');
class yonghu extends \plugin{
	public function member_show($id,$default_url='',$default=false){		
		$member=pdo_get('jy_yxw_yonghu',array('id'=>intval($id),'uniacid'=>$_SESSION['uniacid']),array('id','title','logo','from_type'));
		if(!$member){
			$member=$default?:array('nickname'=>'无');
		}else{
			$member['nickname']=$member['title']?:'无';
			$member['avatar']=tomedia($member['logo']);
		}
		$member['url']='jy_yxw/yonghu.list/look/';
		return $member;
	}
	public function oneadd($postdata,$storeid=0,$noexi=false,$canadd=false){
		$memberid=$this->model('member')->add($postdata,$postdata['fromstoreid'],$noexi,$canadd);
		$postdata['memberid']=$memberid;
		$postdata['storeid']=$_SESSION['storeid'];
		$postdata['classid']=$this->session('classid');
		$yonghuid=$this->addyonghu($postdata,0,0,true);
		return $yonghuid;
	}
	public function addyonghu($postdata,$storeid=0,$status=0,$isoneadd=false){
		if(!$postdata['memberid']){
			exi('请选择要分班的客户！','error');
		}
		if(!$postdata['classid']){
			exi('请选择售后群！','error');
		}
		$class=$this->get('class',array('id'=>$postdata['classid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$status){
			if($class['status']==1){
				$status=12;
			}elseif($class['status']==2){
				$status=13;
			}elseif($class['status']==3){
				$status=14;
			}else{
				$status=11;
			}
		}
		if($isoneadd){
			pdo_update('jy_yxw_member',array('status'=>$status),array('id'=>$postdata['memberid'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($postdata['bao_day']){
			$postdata['baotime']=strtotime($postdata['bao_day']);
		}
		$data=array(
			'memberid'=>$postdata['memberid'],
			'classid'=>$postdata['classid'],
			'title'=>$postdata['title'],
			'mobile'=>$postdata['mobile'],
			'mobile2'=>$postdata['mobile2'],
			'from_type'=>$postdata['from_type'],
			'title'=>$postdata['title'],
			'sn'=>'SN'.get_ordersn($this->tablename('yonghu'),'sn'),
			'fuwuid'=>$class['fuwuid'],
			'status'=>$status?:11,
			'jiguan'=>$postdata['jiguan']?:$postdata['pcd']['province'],
			'danfee'=>$postdata['danfee'],
			'bufee'=>$postdata['bufee'],
			'baotime'=>$postdata['baotime'],
			'pay_typetitle'=>$postdata['pay_typetitle'],
			'zhushu'=>$postdata['zhushu'],
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['storeid']=$storeid?:$_SESSION['storeid'];
		$data['fromstoreid']=$postdata['fromstoreid']?:$_SESSION['storeid'];
		$data['createtime']=time();
		pdo_insert('jy_yxw_yonghu',$data);
		$id=pdo_insertid();
		return $id;
	}
	public function dangandown($yonghu=array(),$filename=''){
		$name=$yonghu['title'];
		if(!$name){
			return false;
		}
		@mkdir(ROOT_AT.$filename.'/'.$name);
		$newfilename=ROOT_AT.$filename.'/'.$name.'/';
		if($yonghu['danganimage1']){
			$image=ROOT_AT.$yonghu['danganimage1'];
			$ext=substr(strrchr($yonghu['danganimage1'],'.'),1);
			$newName=$newfilename."档案照片A面.".$ext;
			$copied=copy($image,$newName);
		}
		if($yonghu['danganimage2']){
			$image=ROOT_AT.$yonghu['danganimage2'];
			$ext=substr(strrchr($yonghu['danganimage2'],'.'),1);
			$newName=$newfilename."档案照片B面.".$ext;
			$copied=copy($image,$newName);
		}
		return $copied;
	}
	public function downimages($images=array(),$filename='',$yonghuname='',$imgname=''){
		$imgname=$imgname?:'图片';
		if(!$yonghuname){
			return false;
		}
		@mkdir(ROOT_AT.$filename.'/'.$yonghuname);
		$newfilename=ROOT_AT.$filename.'/'.$yonghuname.'/';
		foreach($images as $k=>$img){
			$sn=$k+1;
			$image=ROOT_AT.$img;
			$ext=substr(strrchr($img,'.'),1);
			$newName=$newfilename.$imgname.$sn.".".$ext;
			$copied=copy($image,$newName);
		}
		return $copied;
	}
	public function upclassxs_status($classid=0,$status=1){
		if(!$classid){
			return false;
		}
		$update=array();
		if($status==0){
			$status3=11;
		}elseif($status==1){
			$status3=12;
			$update['xuetime']=time();
		}elseif($status==2){
			$status3=13;
			$update['biyetime']=time();
		}elseif($status==3){
			$status3=27;
		}
		$update['status']=$status3;
		$params=array('status <'=>14,'classid'=>$classid,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		pdo_update('jy_yxw_yonghu',$update,$params);
		return true;
	}
	public function edit_item($from_type=0,$settype=0){
		$item=array(
			array('type'=>'string','title'=>'售后名称','name'=>'title'),
			array('type'=>'string_two','title'=>'联系电话','title1'=>'手机号','title2'=>'备用手机号','name1'=>'mobile','name2'=>'mobile2'),
			array('type'=>'string_two','title'=>'紧急联系人','title1'=>'电话','title2'=>'关系','name1'=>'jj_mobile','name2'=>'jj_guanxi'),
			array('name'=>'fromstoreid','title'=>'生源门店','type'=>'select','options'=>'stores'),
			array('name'=>'danfee','title'=>'成交费用','type'=>'unitstring','unit'=>'元'),
			array('name'=>'bufee','title'=>'补助费用','type'=>'unitstring','unit'=>'元'),
			array('type'=>'time','title'=>'成交日期','name'=>'bao_day','time_type'=>'day'),
			array('type'=>'string','title'=>'住宿情况','name'=>'zhushu','help'=>''),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array('type'=>'string','title'=>'邮箱','name'=>'email'),
			array('type'=>'string','title'=>'QQ','name'=>'qq'),
			array('type'=>'string','title'=>'专业','name'=>'zhuanye'),
			array('type'=>'string','title'=>'完结学校','name'=>'school'),
			array('type'=>'radio','title'=>'学历','name'=>'xueli','options'=>'xueli_type'),
			array('type'=>'image','title'=>'学信网/证书','name'=>'xueliimg','help'=>''),
			array('type'=>'string','title'=>'工作经验','name'=>'workdesc'),
			array('type'=>'radio','title'=>'性别','name'=>'sex','options'=>'sex'),
			array('type'=>'string_two','title'=>'年龄','title1'=>'出生年分','title2'=>'年龄','name1'=>'birth','name2'=>'age','read2'=>true),
			array('type'=>'pcd','title'=>'户籍','name'=>'pcd','help'=>''),
			array('type'=>'string','title'=>'身份证号码','name'=>'cardno','help'=>''),
			array('type'=>'image','title'=>'身份证正面','name'=>'cardimg1','help'=>''),
			array('type'=>'image','title'=>'身份证背面','name'=>'cardimg2','help'=>''),
			array('type'=>'string','title'=>'详细地址','name'=>'address','help'=>''),
			array('type'=>'string','title'=>'银行卡号','name'=>'bankno','help'=>''),
			array('type'=>'string','title'=>'银行支行','name'=>'banktitle','help'=>''),
			array('type'=>'image','title'=>'银行卡正面照片','name'=>'bankimg','help'=>''),
		);
		return $item;
	}
	public function daoru($post=array()){
		if(!$post['excel']){
			exi('请先上传CSV文件！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel($localexcel);
		$res=$this->model('model')->get_csvitems($excel);
		if(!$res[0]['联系电话'] && !$res[0]['姓名']){
			$excel=cfc('tools')->importExcel($localexcel,true);
			$res=$this->model('model')->get_csvitems($excel);
			if(!$res[0]['联系电话'] && !$res[0]['姓名']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		cfc('files')->source_del($post['excel']);
		$cdata=$this->csv_add($res);
		if($cdata){
			cache_write('cdata',$cdata,'jy_yxw'.$_SESSION['workerid']);
			exi('导入成功（电话重复的会自动忽略）！','','/member.cdata/');
		}else{
			exi('导入成功！');
		}
	}
	public function csv_add($rows){
		$cdata=array();
		foreach($rows as $item){
			if(!$item['姓名']){
				//exi('有数据不存在姓名！','error');
			}
			$data=array(
				'title'=>trim($item['姓名']),
				'mobile'=>trim($item['联系电话']),
				'zhuanye'=>trim($item['专业']),
				'cardno'=>trim($item['身份证号']),
				'jiguan'=>trim($item['籍贯']),
				'school'=>trim($item['完结院校']),
				'jj_mobile'=>trim($item['紧急联系电话']),
				'bao_day'=>trim($item['成交试用时间']),
				'danfee'=>trim($item['单费']),
				'bufee'=>trim($item['补助情况']),
				'pay_typetitle'=>trim($item['缴费方式']),
				'zhushu'=>trim($item['住宿情况']),
			);
			if($item['门店']){
				$data['fromstoreid']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'title like'=>'%'.trim($item['门店']).'%'),'id');
			}
			if($item['销售人']){
				$data['worker6id']=pdo_getcolumn('jy_yxw_worker',array('juese'=>6,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'title'=>trim($item['销售人'])),'id');
			}
			if($item['性别']){
				$item['性别']=trim($item['性别']);
				$data['sex']=$item['性别']=='男'?1:2;
			}
			if($item['年龄']){
				$age=intval($item['年龄']);
				$birth=date('Y');
				$birth -=$age;
				$data['birth']=$birth;
			}
			$xueli_type=$this->xueli_type;
			foreach($xueli_type as $k=>$xv){
				if(strpos($xv,$item['学历']) !== false){
					$data['xueli']=$k;
				}
			}
			//成交/定金重复
			$isadd=pdo_get('jy_yxw_yonghu',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$data['mobile']),array('id','title'));
			if($isadd['id']){
				$cdata[]=array('title'=>$data['title'],'mobile'=>$data['mobile'],'type'=>1);
				continue;
			}
			$this->oneadd($data,0,true,true);
		}
		return $cdata;
	}
}
?>